﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MrBrooks.Utility
{
    /// <summary>
    /// 转换工具
    /// </summary>
    public static class Transfer
    {
        #region .数字格式化.
        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private static string FormatNumber3(decimal number)
        {
            return String.Format("{0:n3}", number);
        }

        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private static string FormatNumber2(decimal number)
        {
            return String.Format("{0:N2}", number);
        }
        #endregion

        #region .日期时间.
        /// <summary>
        /// 北京时间 ，UTC时间+8
        /// </summary>
        private const int UTC_TIME_ZONE = 8;

        /// <summary>
        /// 将UTC时间转换为北京时间，并格式化为("yyyy-MM-dd HH:mm:ss"),格式
        /// </summary>
        /// <param name="utcTime"></param>
        /// <returns></returns>
        public static string TransUtcTime(DateTime utcTime)
        {
            return utcTime.AddHours(UTC_TIME_ZONE).ToString("yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region .随机数序列.
        /// <summary>
        /// 随机数
        /// </summary>
        private static string[] randomChars = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        private static int randomCharsLength = randomChars.Length;
        //private static Random random = new Random(int.Parse(DateTime.Now.ToString("smHd")));
        /// <summary>
        /// 随机种子
        /// </summary>
        private static int randomSeed = Guid.NewGuid().GetHashCode();

        /// <summary>
        /// 随机数产生器
        /// </summary>
        private static Random random = new Random(randomSeed);

        /// <summary>
        /// 产生固定长度的随机字符串
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string RandomKey(int length)
        {
            int len = randomCharsLength;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                int idx = random.Next(0, len);
                sb.Append(randomChars[idx]);
            }

            return sb.ToString();
        }
        #endregion
    }
}
